package com.ruoyi.web.wx.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.service.IOrderService;
import com.ruoyi.user.domain.User;
import com.ruoyi.wx.utils.GetUserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "创建订单支付")
@RestController
@RequestMapping("/wx/orderPay")
public class WxOrderPayController {

    @Autowired
    private IOrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping("/createOrder")
    public AjaxResult pay(HttpServletRequest request, @RequestBody Order order) {
        User loginUser = GetUserUtils.getLoginUser(request);
        order.setUserId(loginUser.getUserId());
        return AjaxResult.success(orderService.createOrder(order));
    }

    @ApiOperation("微信小程序支付")
    @PostMapping("/weChatAppletPay/{orderId}")
    public AjaxResult orderPay(@PathVariable String orderId) {
        return AjaxResult.success(orderService.orderPay(orderId));
    }

    /**
     * 支付成功回调
     */
    @PostMapping("/paySuccessCallback")
    public String paySuccessCallback(HttpServletRequest request) {
        return JSON.toJSONString(orderService.paySuccessCallback(request));
    }

    /**
     * 订单申请退款
     */
    @ApiOperation("订单申请退款")
    @GetMapping("/refundOrder")
    public AjaxResult refundOrderItem(@RequestParam("orderId") String orderId)
    {
        return AjaxResult.success(orderService.refundOrder(orderId));
    }

    /**
     * 退款成功回调
     */
    @PostMapping("/refundSuccessCallback")
    public String refundSuccessCallback(HttpServletRequest request)
    {
        return JSON.toJSONString(orderService.refundSuccessCallback(request));
    }
}
